<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增或编辑组织机构</title>
    <link th:replace="template/sys_header :: header"/>

</head>
<body>
<form lay-filter="form1" class="layui-form margintop20" action="">
    <input type="hidden" id="orgId" name="orgId" value=""/>
    <input type="hidden" id="parentId" name="parentId" value=""/>
    <div class="layui-row margintop15">
        <div class="layui-col-sm2 layui-row-label">
            <span class="x-red">*</span>组织机构名称
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="orgName" name="orgName" lay-verify="required" autocomplete="" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux" style="margin-left: 5px;">
            <span class="x-red">*</span>将会成为组织的名称
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            父级机构
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="parentOrgName" autocomplete="" disabled="disabled" class="layui-input disabled">
            <div style="position: absolute;right: 0;z-index: 1;top: 0;">
                <button type="button" onclick="xadmin.open('选择组织','/sys/organization/selectOrg',600, 650)" class="layui-btn layui-btn-normal">选择组织</button>
            </div>
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            组织机构简称
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="orgShortName" name="orgShortName"  autocomplete="" class="layui-input">
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            机构编号
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="orgCode" name="orgCode" autocomplete="" class="layui-input">
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            排序号
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="orderNum" name="orderNum" lay-verify="number" autocomplete="" class="layui-input">
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            备注
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="remark" name="remark" autocomplete="" class="layui-input">
        </div>
    </div>

    <div class="layui-row margintop15">
        <div class="layui-col-sm2 layui-row-label">

        </div>
        <div class="layui-col-sm5" style="text-align: center">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="save"><i class="iconfont">&#xe747;</i>
                &nbsp;保存&nbsp;
            </button>
            &nbsp;&nbsp;
            <button class="layui-btn layui-btn-danger" onclick="closeWindow()"><i class="iconfont">&#xe747;</i>
                &nbsp;关闭&nbsp;
            </button>
        </div>
    </div>
</form>
<script type="text/javascript" th:replace="template/sys_header :: footer"></script>
<script type="text/javascript" th:inline="javascript">

    var table = layui.table, $ = layui.jquery, form = layui.form;
    var parentId = queryString("parentId");
    var parentOrgName = decodeURIComponent(queryString("parentOrgName"));
    var organization = [[${org}]];//组织机构信息
    var isEdit = false;//默认不是编辑模式
    console.log(organization);
    getById("parentOrgName").value = parentOrgName;
    getById("parentId").value = parentId;
    if(organization && organization.orgId){
        isEdit = true;//编辑
        form.val('form1', {
            "orgId" : organization.orgId,
            "parentId": organization.parentId,
            "orgName": organization.orgName,
            "orgShortName": organization.orgShortName,
            "orgCode": organization.orgCode,
            "orderNum": organization.orderNum,
            "remark": organization.remark
        });
    }

    $(function () {
        form.on('submit(save)', function (data) {
            console.log(data.field);
            //发异步，数据提交
            if(isEdit){
                edit(data.field);
            }else{
                add(data.field);
            }
            return false;
        });
    })

    //关闭当前frame
    function closeWindow() {
        xadmin.close();
    }
    
    function add(field) {
        console.log("add...");
        new AjaxRequest({
            type: "POST",
            url: webroot + "sys/organization/add",
            data: JSON.stringify(field),
            isShowLoader: true,
            success: function (res) {
                //console.log(res);
                if (res) {
                    if (res.resultCode == "0000") {
                        var orgId = res.resultObj;
                        layer.alert("保存成功", {icon: 1}, function () {
                            //xadmin.father_reload();//刷新父窗体
                            xadmin.getParentWindow().refreshlist(orgId , field.orgName , true);
                            xadmin.close();//关闭当前frame
                        });
                    } else {
                        layer.alert(res.resultMessage, {icon: 2})
                    }
                }
            }
        });
    }
    
    function edit(field) {
        new AjaxRequest({
            type: "POST",
            url: webroot + "sys/organization/edit",
            data: JSON.stringify(field),
            isShowLoader: true,
            success: function (res) {
                //console.log(res);
                if (res) {
                    if (res.resultCode == "0000") {
                        layer.alert("保存成功", {icon: 1}, function () {
                            //xadmin.father_reload();//刷新父窗体
                            xadmin.getParentWindow().refreshlist(field.orgId , field.orgName , false);
                            xadmin.close();//关闭当前frame
                        });
                    } else {
                        layer.alert(res.resultMessage, {icon: 2})
                    }
                }
            }
        });
    }

    //子窗体选择组织机构后调用此方法
    function selectOrg(parentId , parentOrgName) {
        getById("parentId").value = parentId;
        getById("parentOrgName").value = parentOrgName;
    }
</script>
</body>
</html>